Skip to content

Added monte carlo javascript example #175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jun 30, 2018
Merged

Added monte carlo javascript example #175

merged 9 commits into from
Jun 30, 2018

Conversation

xam4lor
Copy link
Contributor

@xam4lor xam4lor commented Jun 29, 2018

No description provided.

Copy link
Contributor

@Butt4cak3 Butt4cak3 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow! You're really putting in some work. Thanks for another request!

First off, we use 2 space indentation for JavaScript in the Algorithm Archive.

We have a .editorconfig file in the repo, so if you want, you can install the EditorConfig plugin for your editor of choice and never have to worry about indentation inside the AAA again. You don't have to, though.

@@ -0,0 +1,26 @@
// submitted by xam4lor
function inCircle(xPos, yPos, radius) {
if(xPos * xPos + yPos * yPos < radius * radius) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could just do this:

function inCircle(xPos, yPos, radius) {
  return xPos * xPos + yPos * yPos < radius * radius;
}

}

let piEstimate = 4 * Math.PI / (n * (radius * radius));
console.log('Percent error is: %s%', piEstimate);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's one "%" too much here.

Copy link
Contributor Author

@xam4lor xam4lor Jun 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, because the %s is to insert the string et the last % is because the value is in % !

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it shows, for example : "Percent error is: 0.2%"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, right. My bad.

@Butt4cak3 Butt4cak3 self-assigned this Jun 29, 2018
Copy link
Contributor

@zsparal zsparal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mainly const/let/var nitpicks, other than that the implementation looks good. For the formatting I recommend installing prettier for your editor, it'll make your life much easier.

function monteCarlo(n, radius) {
let piCount = 0;

for (var i = 1; i < n; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That var should be a let

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, it should probably start from 0 if you want n iterations

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I not catch this? Maybe I should think about going to bed.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How did I not too 🤣

let pointX = Math.random();
let pointY = Math.random();

if(inCircle(pointX, pointY, radius)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

JS also has spaces around parens: if (...)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. At this point I'm just handing this over to you. You seem to be in a much clearer state of mind than I am right now.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was a mistake !

let piCount = 0;

for (var i = 1; i < n; i++) {
let pointX = Math.random();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pointX and pointY should be const instead of let

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about commenting this, but I think it's more of a preference thing, isn't it? I don't think we ever decided to enforce this style in the AAA.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's recommended by pretty much every JS/TS linter on the default settings. I think it's a reasonable request.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The discussion about using const vs let is almost just for readability but It's a reasonable request, as you said.

}
}

let piEstimate = 4 * Math.PI / (n * (radius * radius));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be const

@Butt4cak3 Butt4cak3 added the Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.) label Jun 29, 2018
@xam4lor
Copy link
Contributor Author

xam4lor commented Jun 30, 2018

I really like this project because It has so much potential! And I'm almost in holidays, so ... :)

@leios
Copy link
Member

leios commented Jun 30, 2018

@xam4lor Thanks for all the help so far! We have a lot left to do, but definitely growing!

@xam4lor
Copy link
Contributor Author

xam4lor commented Jun 30, 2018

You can thanks 3blue1brown and this video for speaking about you too!

@leios
Copy link
Member

leios commented Jun 30, 2018

@xam4lor yeah, that shoutout really boosted the number of people contributing to this project. I'm pretty excited to see where we go from here! (sorry for the off-topic conversation)

@zsparal
Copy link
Contributor

zsparal commented Jun 30, 2018

It seems like all we're changing all the Monte Carlo implementations to get rid of the "radius" variable. Could you take a look at the Julia one and make similar changes in the JS implementations as well?

@xam4lor
Copy link
Contributor Author

xam4lor commented Jun 30, 2018

Me too!
Done!

@zsparal
Copy link
Contributor

zsparal commented Jun 30, 2018

@Butt4cak3 could you approve and merge this one? Your change request is blocking the merge from the web UI and I don't have a desktop now.

@Butt4cak3 Butt4cak3 merged commit 8367b34 into algorithm-archivists:master Jun 30, 2018
@Butt4cak3
Copy link
Contributor

@Gustorn Oops. Thought I already approved.

@xam4lor xam4lor deleted the monte-carlo-javascript branch June 30, 2018 22:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Implementation This provides an implementation for an algorithm. (Code and maybe md files are edited.)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants